package org.javia.arity;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class UtilTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testShortApprox() {
		assertEquals(1.33,    Util.shortApprox(1.3333333333, 0.01)    , 0.09);
		assertEquals(1.3333,  Util.shortApprox(1.3333333333, 0.0001)  , 0.0009);
		assertEquals(-1.33333, Util.shortApprox(-1.3333333333, 0.000001), 0.000009);
	}

	@Test
	public void testSizeTruncate() {
		assertEquals("1E8",Util.sizeTruncate("123456789", 3));
		assertEquals("200",Util.sizeTruncate("200",3));
		assertEquals("2.00E10",Util.sizeTruncate("2.000000004E10", 7));
		assertEquals("2.12345678E10",Util.sizeTruncate("2.12345678E10", 0));
		assertEquals("2.12345678E10",Util.sizeTruncate("2.12345678E10", -2));
		assertEquals("2.12345678E10",Util.sizeTruncate("2.12345678E10", 100));
		assertEquals("-21234567810",Util.sizeTruncate("-21234567810", 100));
		assertEquals("21234567810",Util.sizeTruncate("21234567810", 100));
		assertEquals("-2.",Util.sizeTruncate("-2.123456780", 3)); ///BUG
	}

	@Test
	public void testDoubleToStringDoubleInt() {
//		assertEquals("1.2345679E12",Util.doubleToString(1234567899999.0, 7));
//		assertEquals("1.23457E14",Util.doubleToString(123456789012345.0,10 ));
		assertEquals("-1.23457E14",Util.doubleToString(-123456789012345.0,10 ));
//		assertEquals("1.23456789012345E14",Util.doubleToString(123456789012345.0,Util.LEN_UNLIMITED )); //max size
		assertEquals("1.23456788E14",Util.doubleToString(123456789012345.0,Util.FLOAT_PRECISION));
		assertEquals("22",Util.doubleToString(22,1 ));
		assertEquals("2.222E-12",Util.doubleToString(0.000000000002222,2 ));
		
		
		
	}

	@Test
	public void testDoubleToStringDoubleIntInt() {
		assertEquals("1.9E13",Util.doubleToString(19999999999999.0,6,2 ));
		
	}

	@Test
	public void testComplexToString() {
		Complex complex= new Complex(1.12345678, -0.98765432);
		assertEquals("1.123-0.9i",Util.complexToString(complex, 10, 2));
		assertEquals("1.12-0.9i",Util.complexToString(complex, 9, 2));
		assertEquals("1",Util.complexToString(new Complex(1,0), 9, 2));
		assertEquals("1+i",Util.complexToString(new Complex(1,1), 9, 2));
		assertEquals("-1-i",Util.complexToString(new Complex(-1,-1), 9, 2));
		assertEquals("NaN",Util.complexToString(new Complex(Double.NaN,Double.NaN), 10, 2));
		assertEquals("Infinity",Util.complexToString(new Complex((double)4/0,1), 10, 2));
		assertEquals("Infinity*i",Util.complexToString(new Complex(1,(double)4/0), 10, 2));
	}

}
